package cn.ava.ymcc.mq.consumer;

import cn.ava.ymcc.constants.CommonConstants;
import cn.ava.ymcc.service.IKillCourseService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.redisson.api.RFuture;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Map;

@Component
@Slf4j
@RocketMQMessageListener(
        consumerGroup = "service-kill-course-timeout-consumer",//消费者组
        topic = "kill-topic",//topic
        selectorExpression = "timeout-tags",//tags
        messageModel = MessageModel.CLUSTERING
        // 广播模式，一个消息可以被多个消费者消费
        //集群模式，一个消息只会给到一个消费者

)
public class ConsumerKillCourseDelayMessage implements RocketMQListener<MessageExt> {

    @Autowired
    private IKillCourseService killCourseService;

    @Override
    public void onMessage(MessageExt messageExt) {
        Map map = null;
        try {
            String jsonResult = new String(messageExt.getBody(), StandardCharsets.UTF_8);
            map = JSONObject.parseObject(jsonResult, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        killCourseService.timeoutCancel(map);
    }

}
